package com.tanhua.sso.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;

import java.util.Date;

/**
 * @Description AUTO
 * @ClassName MyMetaObjectHandler
 * @Author ZhengXinjie
 * @date 2021.02.20 09:43
 */
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object created = getFieldValByName("created", metaObject);
        if(created == null){
            setFieldValByName("created", new Date(), metaObject);
        }
        Object updated = getFieldValByName("updated", metaObject);
        if(null == updated){
            // 如果字段为空, 则可以填充
            setFieldValByName("updated", new Date(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 无需判定是否为空, 直接填充
        setFieldValByName("updated", new Date(), metaObject);
    }
}

